package com.example.demo.controller;


import com.example.demo.entity.Admin;
import com.example.demo.service.IAdminService;
import com.example.demo.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author hyb
 * @since 2024-12-24
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService iAdminServiceService;

    @GetMapping("/list")
    public List<Admin> list() {return iAdminServiceService.list();}

    //新增
    @PostMapping("/save")
    public boolean save(@RequestBody Admin admin) {
        try {
            // 尝试保存管理员信息
            return iAdminServiceService.save(admin);
        } catch (Exception e) {
            // 记录异常信息
            //logger.error("Error saving admin", e);
            // 返回false表示保存失败
            return false;
        }
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Admin admin) {return iAdminServiceService.updateById(admin);}
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody Admin admin) {return iAdminServiceService.saveOrUpdate(admin);}
    //删除
    @GetMapping("/delete")
    public boolean saveOrMod(Integer admin_id) {return iAdminServiceService.removeById(admin_id);}
    //查询（模糊，匹配）


    @GetMapping("/{id}")
    public Admin getAdminById(@PathVariable Integer id) {
        return iAdminServiceService.getById(id);
    }
    @PostMapping("/create")
    public Admin create(@RequestBody Admin admin) {
        iAdminServiceService.save(admin);
        return admin;
    }
    @PostMapping("/create_default")
    public Admin create_default() {
        Admin admin = new Admin(null, "123456", 0, "20040530zmy@gmail.com", 1, LocalDateTime.now(), LocalDateTime.now());
        iAdminServiceService.save(admin);
        return admin;
    }

}
